package tests;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.WebDriverWait;

import javax.swing.*;
import java.time.Duration;

public class EditPage extends Utils {
    public static String url = " http://8.137.19.140:9090/blog_edit.html ";
    public EditPage() {
        super(url);
    }

    /*
    * 检查发布博客成功
    * */
    public void EditSuc() throws InterruptedException {
        //无法输入博客内容怎么办？
        //两个解决办法：

        /*
        * 方法一：
        * 博客内容本身就有默认内容，我们不需要通过手动实现
        * 只需要输入标题，然后点击发布文章即可
        * */

        String blogTitle = "自动化测试";
        driver.findElement(By.cssSelector("#title")).sendKeys(blogTitle);
        driver.findElement(By.cssSelector("#submit")).click();
        driver.findElement(By.cssSelector("body > div.container > div.left > div"));
//        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(2));
        Thread.sleep(2000);
        //检查一下是否发布成功
        String curTitle = driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(6) > div.title")).getText();
        assert curTitle.equals(blogTitle);

        driver.quit();

        /*
        * 方法二：
        * 将鼠标挪动到博客内容区域
        * 双击鼠标全选，并删除内容：鼠标双击，键盘DEL
        * 输入正文内容
        * */

        //首先先要查找到这个元素
        WebElement ele = driver.findElement((By.cssSelector("#editor > div.CodeMirror.cm-s-default.CodeMirror-wrap")));

        Actions actions = new Actions(driver);
        //双击一下
        //perform 的作用，为了在页面看出效果
        Thread.sleep(3000);
        actions.doubleClick(ele).perform();
        Thread.sleep(3000);
        actions.keyDown(Keys.DELETE).perform();
        Thread.sleep(3000);
        actions.moveToElement(ele).sendKeys("键盘鼠标操作输入博客内容").perform();
        Thread.sleep(3000);
        driver.findElement(By.cssSelector("#submit")).click();
        Thread.sleep(3000);

        driver.quit();
    }
}
